import { DD_SYSTEM } from '@/constants/api-pre';
import { request } from '@/service/request';

export type DataSourceConfig = Api.Common.CommonRecord<{
  name: string;

  code: string;

  remark: string;

  dbType: string;

  dbDriver: string;

  dbUrl: string;

  dbName: string;

  dbUsername: string;

  dbPassword: string;

  createTime: string;
}>;

export type DataSourceConfigEditAndCreateParams = CommonType.RecordNullable<
  Pick<
    DataSourceConfig,
    'name' | 'code' | 'remark' | 'dbType' | 'dbDriver' | 'dbUrl' | 'dbName' | 'dbUsername' | 'dbPassword'
  >
>;

export type DataSourceConfigList = Api.Common.PaginatingQueryRecord<DataSourceConfig>;

export type DataSourceConfigSearchParams = CommonType.RecordNullable<
  Pick<DataSourceConfig, 'name' | 'dbType'> & Api.Common.CommonSearchParams
>;

export function getFetchDataSourceConfigUrl(id: number) {
  return `${DD_SYSTEM}/data-source-config/${id}/form`;
}

export function fetchDataSourceConfigList(params?: DataSourceConfigSearchParams) {
  return request<DataSourceConfigList>({
    url: `${DD_SYSTEM}/data-source-config/page`,
    method: 'get',
    params
  });
}
export function fetchDataSourceConfigOptions() {
  return request<CommonType.Option<number>[]>({
    url: `${DD_SYSTEM}/data-source-config/options`,
    method: 'get'
  });
}
export function createDataSourceConfig(data: any) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/data-source-config`,
    method: 'post',
    data
  });
}
export function updateDataSourceConfig(id: number, data?: any) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/data-source-config/${id}`,
    method: 'put',
    data
  });
}
export function deleteDataSourceConfig(id: string) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/data-source-config/${id}`,
    method: 'delete'
  });
}

export function dbTestConnection(data: any) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/data-source-config/test-connection`,
    method: 'post',
    data
  });
}

export const DbDriverMap = {
  // MySQL 数据库
  '1': { dbDriver: 'com.mysql.jdbc.Driver' },
  // MySQL5.7+ 数据库
  '4': { dbDriver: 'com.mysql.cj.jdbc.Driver' },
  // Oracle
  '2': { dbDriver: 'oracle.jdbc.OracleDriver' },
  // SQLServer 数据库
  '3': { dbDriver: 'com.microsoft.sqlserver.jdbc.SQLServerDriver' },
  // marialDB 数据库
  '5': { dbDriver: 'org.mariadb.jdbc.Driver' },
  // postgresql 数据库
  '6': { dbDriver: 'org.postgresql.Driver' },
  // 达梦 数据库
  '7': { dbDriver: 'dm.jdbc.driver.DmDriver' },
  // 人大金仓 数据库
  '8': { dbDriver: 'com.kingbase8.Driver' },
  // 神通 数据库
  '9': { dbDriver: 'com.oscar.Driver' },
  // SQLite 数据库
  '10': { dbDriver: 'org.sqlite.JDBC' },
  // DB2 数据库
  '11': { dbDriver: 'com.ibm.db2.jcc.DB2Driver' },
  // Hsqldb 数据库
  '12': { dbDriver: 'org.hsqldb.jdbc.JDBCDriver' },
  // Derby 数据库
  '13': { dbDriver: 'org.apache.derby.jdbc.ClientDriver' },
  // H2 数据库
  '14': { dbDriver: 'org.h2.Driver' },
  // 其他数据库
  '15': { dbDriver: '' }
};
export const DbUrlMap = {
  // MySQL 数据库
  '1': { dbUrl: 'jdbc:mysql://127.0.0.1:3306/dimples-cloud?characterEncoding=UTF-8&useUnicode=true&useSSL=false' },
  // MySQL5.7+ 数据库
  '4': {
    dbUrl:
      'jdbc:mysql://127.0.0.1:3306/dimples-cloud?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai'
  },
  // Oracle
  '2': { dbUrl: 'jdbc:oracle:thin:@127.0.0.1:1521:ORCL' },
  // SQLServer 数据库
  '3': { dbUrl: 'jdbc:sqlserver://127.0.0.1:1433;SelectMethod=cursor;DatabaseName=jeecgboot' },
  // Mariadb 数据库
  '5': { dbUrl: 'jdbc:mariadb://127.0.0.1:3306/dimples-cloud?characterEncoding=UTF-8&useSSL=false' },
  // Postgresql 数据库
  '6': { dbUrl: 'jdbc:postgresql://127.0.0.1:5432/dimples-cloud' },
  // 达梦 数据库
  '7': {
    dbUrl:
      'jdbc:dm://127.0.0.1:5236/?dimples-cloud&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8'
  },
  // 人大金仓 数据库
  '8': { dbUrl: 'jdbc:kingbase8://127.0.0.1:54321/dimples-cloud' },
  // 神通 数据库
  '9': { dbUrl: 'jdbc:oscar://192.168.1.125:2003/dimples-cloud' },
  // SQLite 数据库
  '10': { dbUrl: 'jdbc:sqlite://opt/test.db' },
  // DB2 数据库
  '11': { dbUrl: 'jdbc:db2://127.0.0.1:50000/dimples-cloud' },
  // Hsqldb 数据库
  '12': { dbUrl: 'jdbc:hsqldb:hsql://127.0.0.1/dimples-cloud' },
  // Derby 数据库
  '13': { dbUrl: 'jdbc:derby://127.0.0.1:1527/dimples-cloud' },
  // H2 数据库
  '14': { dbUrl: 'jdbc:h2:tcp://127.0.0.1:8082/dimples-cloud' },
  // 其他数据库
  '15': { dbUrl: '' }
};
